<?php

/////////////////////////////////////////////////////////////////
// SpeedPHP Hass    , Copyright (C)      2011-2012 chinhai.com //
/////////////////////////////////////////////////////////////////

$__speedy_compression_level = 9;

/**
 * Hass 前后端通用模板方案使用。
 *
 * speedy拥有的功能：按模板路径来获取模板并显示，通过assign对模板内变量进行赋值，检查模板文件是否存在，GZip压缩等。
 */
class Hass {

    /**
     * 模板目录
     */
    public $template_dir = null;
    private $template_themes_dir = null;

    /**
     * 是否开启GZip压缩
     */
    public $enable_gzip = FALSE;

    /**
     * GZip压缩级别
     */
    public $compression_level = 9;

    /**
     * 不检查编译目录
     */
    public $no_compile_dir = true;

    /**
     *  compile directory
     */
    public $compile_dir = null;

    /**
     *  cache directory
     */
    public $cache_dir = null;
    public $left_delimiter = '{{';
    public $right_delimiter = '}}';

    /**
     * 模板内使用的变量值
     */
    private $_vars = array();

    private $_baseDir = null;
    
    /**
     *  Loader
     */
    public $mustacheLoader = null;

    /**
     *  parser
     */
    public $mustacheParser = null;
    public $sassParser = null;

    /**
     * Class constructor, initializes basic SpeedMustache properties
     */
    public function __construct() {
	$this->_baseDir = dirname(__FILE__);
    }

    /**
     * 对模板赋值
     * @param key 变量名称，或变量数组
     * @param value 变量值
     */
    public function assign($key, $value = null) {
	if (is_array($key)) {
	    foreach ($key as $var => $val)
		if ($var != '')
		    $this->_vars[$var] = $val;
	}else {
	    if ($key != "")
		$this->_vars[$key] = $value;
	}
    }

    /** 兼容Smarty3 */
    public function registerPlugin() {
	
    }

    /**
     * 显示模板
     * @param tplname 模板名称
     */
    public function display($tplname, $extension = 'mustache') {
	$output = '';
	if ($this->template_themes_dir === null) {
	    $this->template_themes_dir = $this->template_dir . ($this->_vars['themes'] ? '/' . $this->_vars['themes'] : '');
	}
	if ($extension === 'mustache') {
	    if ($this->mustacheLoader == null) {
		$this->mustacheLoader = spClass('MustacheLoader', array($this->template_themes_dir), $this->_baseDir . '/mustache.php/MustacheLoader.php');
		$this->mustacheParser = spClass('Mustache', array(
		    null, null, $this->mustacheLoader, array('delimiters' => array($this->left_delimiter, $this->right_delimiter))
			), $this->_baseDir . '/mustache.php/Mustache.php');
	    }
	    $output = $this->mustacheParser->render($this->mustacheLoader[$tplname], $this->_vars);
	} else if ($extension === 'sass') {
	    if ($this->sassParser == null) {
		$this->sassParser = spClass('Mustache', array(
		    null, null, $this->mustacheLoader, array('delimiters' => array($this->left_delimiter, $this->right_delimiter))
			), $this->_baseDir . '/mustache.php/Mustache.php');
	    }
	    $output = $this->sassParser->render($this->$sassLoader[$tplname], $this->_vars);
	}
	echo $output;
	extract($this->_vars);
	/* if(is_readable(realpath($this->template_dir).'/'.$tplname)){
	  $tplpath = realpath($this->template_dir).'/'.$tplname;
	  }elseif(is_readable($tplname)){
	  $tplpath = $tplname;
	  }else{
	  spError("speedy引擎：无法找到模板 ".$tplname);
	  }
	  extract($this->_vars);
	  if( TRUE == $this->enable_gzip ){
	  GLOBAL $__speedy_compression_level;
	  $__speedy_compression_level = $this->compression_level;
	  ob_start('speedy_ob_gzip');
	  }
	  include $tplpath; */
    }

}

function speedy_ob_gzip($content) {
    if (!headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) {
	GLOBAL $__speedy_compression_level;
	$content = gzencode($content, $__speedy_compression_level);
	header("Content-Encoding: gzip");
	header("Vary: Accept-Encoding");
	header("Content-Length: " . strlen($content));
    }
    return $content;
}

